package ATM.frame;

import ATM.service.UserService;
import ATM.service.impl.UserServiceImpl;
import ATM.util.DBUtil;
import ATM.pojo.UserDO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class
ModifyPanel extends JPanel implements ActionListener {
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;

    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;

    private JTextField jTextField1;
    private JTextField jTextField2;
    private JTextField jTextField3;

    private Icon buttonIcon1 = util.Util.getIcon("/images/buttonSure.jpg");
    private Icon buttonIcon2 = util.Util.getIcon("/images/buttonClear.jpg");
    private Icon buttonIcon3 = util.Util.getIcon("/images/buttonOff.jpg");

    private ATMmain atMmain;

    private UserService userService = new UserServiceImpl();

    private int count = 3;

    public ModifyPanel(ATMmain atMmain) {
        this.atMmain = atMmain;
        this.setLayout(null);
        this.setOpaque(false);

       /* jLabel1 = new JLabel("请输入原密码");
        jLabel1.setBounds(300, 230, 250, 50);
        jLabel1.setForeground(Color.blue);
        jLabel1.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(jLabel1);
        jTextField1 = new JTextField("");
        jTextField1.setBounds(500, 235, 200, 30);
        this.add(jTextField1);*/

        jLabel2 = new JLabel("请输入新密码");
        jLabel2.setBounds(250, 290, 300, 50);
        jLabel2.setForeground(Color.blue);
        jLabel2.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(jLabel2);
        jTextField2 = new JTextField("");
        jTextField2.setBounds(500, 295, 200, 30);
        this.add(jTextField2);

       /* jLabel3 = new JLabel("请再次输入，确认密码");
        jLabel3.setBounds(250, 350, 300, 50);
        jLabel3.setForeground(Color.blue);
        jLabel3.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(jLabel3);
        jTextField3 = new JTextField("");
        jTextField3.setBounds(500, 355, 200, 30);
        this.add(jTextField3);*/

        jButton1 = new JButton(buttonIcon1);
        jButton1.setBounds(210, 450, 140, 50);
        this.add(jButton1);
        jButton1.addActionListener(this);

        jButton2 = new JButton(buttonIcon2);
        jButton2.setBounds(450, 450, 140, 50);
        this.add(jButton2);
        jButton2.addActionListener(this);

        jButton3 = new JButton(buttonIcon3);
        jButton3.setBounds(690, 450, 140, 50);
        this.add(jButton3);
        jButton3.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {

            try {
                String pwd = jTextField2.getText();
                userService.modify(atMmain.getLoginUser().getId(),pwd);
                JOptionPane.showMessageDialog(atMmain, "修改成功");

            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,exception.getMessage());
            }



         /*   DBUtil db = new DBUtil();
            UserDO loginUser = db.getById(atMmain.getLoginUser().getId());
            loginUser.setUserPassWord(jTextField2.getText());
            db.update(loginUser);*/

                     /*  Connection con =  util.CreateConnection.getConnection();
                       Statement statement = null ;
                       try {
                           statement= con.createStatement();
                           String sql="update t_user set u_usepsw='"+atMmain.getLoginUser().getUserPassWord()+"' where u_usename='"+atMmain.getLoginUser().getUserName()+"' ";
                           statement.executeUpdate(sql);
                       } catch (SQLException throwables) {
                           throwables.printStackTrace();
                       }finally {
                           util.CloseFunction.close(con,statement);
                       }*/

            //转文件
            //  util.FileChange.ObjectToBinary("111.dat",atMmain.getUserList());

            atMmain.switchPanel("menuPanel");
        } else if (e.getSource() == jButton2) {
            jTextField1.setText("");
            jTextField2.setText("");
            atMmain.switchPanel("modifyPanel");
        } else if (e.getSource() == jButton3) {
            atMmain.switchPanel("loginPanel");
        }
    }
}
